/*===================分类搜索=========================*/
var main = document.querySelector(".dw-filter");
main.onclick = function (event) {
    var ele = event.target;
    if (ele.nodeName == 'A') {
        var lis = ele.parentElement.parentElement.children;
        for (var i = 0; i < lis.length; i++) {
            lis[i].firstElementChild.style.color = "#5c5c5c";
            lis[i].firstElementChild.className = "";
            ele.parentElement.parentElement.parentElement.parentElement.lastElementChild.lastElementChild.innerText = "";
        }
        ele.style.color = "#FF6000";
        ele.className = "pitch";
        var as = document.querySelectorAll(".pitch");
        for (var j = 0; j < as.length; j++) {
            // console.log(as[j].innerText);
            if (as[j].innerText != "所有" && as[j].innerText != "不限") {
                if (ele.parentElement.parentElement.parentElement.parentElement.lastElementChild.lastElementChild.innerText == "") {
                    ele.parentElement.parentElement.parentElement.parentElement.lastElementChild.lastElementChild.innerText = as[j].innerText;
                } else {
                    ele.parentElement.parentElement.parentElement.parentElement.lastElementChild.lastElementChild.innerText += "+" + as[j].innerText;
                }
            }
        }
    }
}

//=========================选择搜索请求数据===========================
var kw =$("#kw").val();//搜索的关键字
var city = $("#city").val();//城市
var publishtime="";//发布时间
var minsalary ="";//最低薪资
var maxsalary=""; //最高薪资
var company ="";//公司性质
var workyear ="";// 工作年限

var degree =""; // 学历
/*var mincompanysize =""//公司人数最小值
var maxcompanysize ="";//公司人数最大值*/

//点击发布时间，获得发布时间
$(".publishtimeUL").click(function (event) {
    var ele = event.target;

    var li = ele.parentElement;
    var lis = li.parentNode.querySelectorAll("li")
    var index = [].indexOf.call(lis,li);
    switch (index) {
        case 0:publishtime ="";break;
        case 1: publishtime = -1;break;
        case 2: publishtime = -3;break;
        case 3: publishtime = -7;break;
        case 4: publishtime = -30;break;
    }
    getall(1);
})
//获得薪资
$(".salaryUL").click(function (event) {
    var ele = event.target;

    var li = ele.parentElement;
    var lis = li.parentNode.querySelectorAll("li")
    var index = [].indexOf.call(lis,li);

    switch (index) {
        case 0:minsalary =0;maxsalary = 50000;break;
        case 1: minsalary = 0;maxsalary = 2000;break;
        case 2: minsalary = 2000;maxsalary = 3000;break;
        case 3: minsalary = 3000;maxsalary = 4500;break;
        case 4: minsalary = 4500;maxsalary = 6000;break;
        case 5: minsalary = 6000;maxsalary = 8000;break;
        case 6:minsalary = 8000;maxsalary = 10000;break;
        case 7: minsalary = 10000;maxsalary = 15000;break;
        case 8: minsalary = 15000;maxsalary = 20000;break;
        case 9: minsalary = 20000;maxsalary = 30000;break;

    }
    getall(1);
})
//暂时不做
$(".companyUL").click(function (event) {
    var ele = event.target;
    company = ele.innerText

    getall(1);
})

//获得工作年限
$(".workyearUL").click(function (event) {
    var ele = event.target;
    var li = ele.parentElement;
    var lis = li.parentNode.querySelectorAll("li")
    var index = [].indexOf.call(lis,li);
    if (index !=0)
        workyear = ele.innerText
    else
        workyear = ""

    getall(1);
})

$(".degreeUL").click(function (event) {
    var ele = event.target
    var li = ele.parentElement;
    var lis = li.parentNode.querySelectorAll("li")
    var index = [].indexOf.call(lis,li);
    if (index !=0)
        degree = ele.innerText
    else
        degree = ""
    getall(1);
})

// ajax 分页查询
function getall(pageNum) {
    var kw =$("#kw").val();
    var city = $("#city").val();//城市
    var url = "/job/all?kw="+kw+"&city="+city+"&publishtime="+publishtime+"&minsalary="+minsalary +
        "&maxsalary="+maxsalary+"&company="+company+"&workyear="+workyear+"&degree="+degree+"&page="+pageNum;
    console.log(url);

    $.ajax({
        type: "GET",
        url: url,
        datatype: "json",
        success: function (result) {

            $(".el-title").css("display","none")
            if (result.status == 0){//没有查找到数据
                $(".job_list_apply").html("");
                $(".job_list_msg").text(result.data)
                console.log(result.data);
            }else {
                $(".el-title").css("display","block")
                $(".job_list_msg").text("")

                $(".job_list_apply").html("");

                var jobCompapagenyList = result.data.list
                var pageNum = result.data.pageNum//当前页数
                var total = result.data.total//总记录数
                var pages = result.data.pages//总页码
                var prePage = result.data.prePage;//上一页
                var nextPage = result.data.nextPage//下一页

                //页码渲染
                $(".pageNum").text(pageNum)
                $(".total").text(total)
                $(".pages").text(pages)
                
                if (pageNum==1){
                    $(".prePage").css("display","none")
                }else {
                    $(".prePage").css("display","inline-block")
                    var hrefVal = "javascript:getall("+(prePage)+")"
                    $(".prePage").attr("href",hrefVal)
                }

                if (pageNum==pages){
                    $(".nextPage").css("display","none")
                }else {
                    $(".nextPage").css("display","inline-block")
                    var hrefVal = "javascript:getall("+(nextPage)+")"
                    $(".nextPage").attr("href",hrefVal)
                }

                //渲染职位信息
                $.each(jobCompapagenyList,function (index,jobCompany) {
                    var jobName = jobCompany.job.jobName;
                    var companyName = jobCompany.company.companyName;
                    var jobCity = jobCompany.job.jobCity;
                    var minSalary = jobCompany.job.jobMinSalary;
                    var maxSalary = jobCompany.job.jobMaxSalary;
                    var publishTimeMS = jobCompany.job.jobPublishTime;
                    var publishTime =getFullDate(publishTimeMS);
                    var jobId = jobCompany.job.jobId
                    var companyId = jobCompany.company.companyId
                    var ele = "<div class=\"el\">\n" +
                        "        <p class=\"t1\">\n" +
                        "            <input class=\"checkbox\" type=\"checkbox\">\n" +
                        "            <span>\n" +
                        "                    <a href='/job/"+jobId+"' target=\"_blank\">"+jobName+"</a>\n" +
                        "                </span>\n" +
                        "        </p>\n" +
                        "        <span class=\"t2\">\n" +
                        "                <a href='/company/"+companyId+"' target=\"_blank\">"+companyName+"</a>\n" +
                        "            </span>\n" +
                        "        <span class=\"t3\">"+jobCity+"</span>\n" +
                        "        <span class=\"t4\">"+minSalary+"/"+maxSalary+"月"+"</span>\n" +
                        "        <span class=\"t5\">"+publishTime+"</span>\n" +
                        "    </div>"

                    $(".job_list_apply").append(ele)
                })

            }

        }
    })
}

//毫秒转换成日期格式
function getFullDate(ms) {
    var d = new Date(ms);
    var year = d.getFullYear()
    var month = d.getMonth()+1
    var day = d.getDate()
    return year+"-"+month+"-"+day

}
//点击搜索按钮
$(".search_btn").click(function () {
    console.log($("#kw").val());
    getall(1);
});

//==================首页点击按钮传过来的值============================================
//获得url后面的kw
var url = window.location.href  ///job/all.jsp?kw=kw
var num = url.indexOf("?");
url = url.substr(num+1);//kw=kw
console.log("url:"+url)

var kw = ""
console.log("kw:"+kw)
if (url.indexOf("kw") != -1){
    kw = url.split("=")[1];//(String)kw[0],(值)kw[1]
     kw = decodeURIComponent(kw);
    console.log(kw);
    $("#kw").val(kw)
}
getall(1);




/*====================全选=============================*/
var selectAllEle = document.querySelector(".select-all-first");
selectAllEle.onclick = function () {
    var ckboxes = document.querySelectorAll(".el input[type=checkbox]");
    for (var i=0;i<ckboxes.length;i++){
        ckboxes[i].checked = selectAllEle.checked;
    }
}


